import Vue from "vue";

/**
 * @description: 包含-文件名转为驼峰命名
 * @param { String } fileName 文件名称
 */
function fileLineToHumpName(fileName) {
  let fileStr = "",
    _fileArr = [];
  if (fileName.includes("-")) _fileArr = fileName.split("-");
  else _fileArr = [fileName];
  const caseArr = _fileArr.map((str) =>
    str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
  );
  caseArr.forEach((item) => (fileStr += item));
  return fileStr || fileName;
}

//component
//递归查找vue文件
const requireComponent = require.context("@/components/common", true, /\.vue$/);
const modulesComponent = requireComponent.keys().map((path) => {
  const component = {};
  const moduleName = path.replace(/^\.\/(.*)\.\w+$/, "$1");
  component[moduleName] = requireComponent(path).default;
  return component;
});
modulesComponent.map((component) =>
  Object.keys(component).forEach((name) =>
    Vue.component(fileLineToHumpName(name), component[name])
  )
);
